import json
import pytest

# TODO 1、测试报告html（无代码）
# TODO 2、控制函数执行顺序
'''
结果：
    方法1-2
    类2--方法
    方法1-3
    方法1-1

@pytest.mark.run(order=2)
class TestDemo1(object):

    @pytest.mark.run(order=3)
    def test_method1(self):
        print("方法1-1")

    @pytest.mark.run(order=1)
    def test_method2(self):
        print("方法1-2")

    @pytest.mark.run(order=2)
    def test_method3(self):
        print("方法1-3")

@pytest.mark.run(order=1)
class TestDemo2(object):
    def test_method(self):
        print("类2--方法")

if __name__ == '__main__':
    pytest.main(['-s', '6-4-11 代码 - 常用插件.py'])
'''
# TODO 3、失败重试
'''
结果：
    FAILED pytest_case/test_case4.py::TestDemo::test_method1 - ZeroDivisionError: division by zero
    1 failed, 1 warning, 1 rerun in 0.20s

class TestDemo(object):
    def test_method1(self):
        print(1/0)
'''
# TODO 4、高级用法
'''
# ① 跳过测试函数
结果：
    方法1-1
    s 
    方法1-3
    s 

version = 25
class TestDemo1(object):

    def test_method1(self):
        print("方法1-1")

    @pytest.mark.skipif(version >= 25, reason="当前版本不执行")
    def test_method2(self):
        print("方法1-2")

    def test_method3(self):
        print("方法1-3")

@pytest.mark.skipif(version >= 25, reason="当前版本不执行")
class TestDemo2(object):

    def test_method2(self):
        print("类2-方法")

# ② 参数化

# 11、单个参数

class TestDemo(object):

    @pytest.mark.parametrize("name", ["小明", "小张"])
    def test_method(self, name):
        print("获取的名字是：", name)

if __name__ == '__main__':
    pytest.main(['-s', "6-4-11 代码 - 常用插件.py"])

# 12、多个参数

class TestDemo(object):

    @pytest.mark.parametrize("name, age", [("小明", 17), ("小张", 19)])
    def test_method(self, name, age):
        print(f"{name}的年龄是：{age}")

if __name__ == '__main__':
    pytest.main(['-s', "6-4-11 代码 - 常用插件.py"])

# 13、通过函数/方法引入测试数据

def build_data():
    with open("6-4-13 build_data.json", encoding="utf-8") as file:
        list = []
        data = json.load(file)
        for i in data:
            new_data = (i.get("username"), i.get("password"), i.get("code"))
            list.append(new_data)
        return list

class TestDemo(object):

    @pytest.mark.parametrize("username, password, code", build_data())
    def test_method(self, username, password, code):
        print(f"账号：{username}, 密码：{password}，登录状态：{code}")

if __name__ == '__main__':
    build_data()

# ③ 断言

def add_func(num1, num2):
    return num1 + num2

class TestDemo(object):
    def test_method(self):
        result = add_func(1, 2)
        assert 4 == result

if __name__ == '__main__':
    pytest.main(['-s'], '6-4-11 代码 - 常用插件.py')
'''